package com.flute.icrawler.app.monitor;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class CrawlMonitorConfig {

	public CrawlMonitorConfig() {
	}

	private Map<String, Method> itemToGetMethod = new HashMap<String, Method>();

	private Map<String, Method> itemToSetMethod = new HashMap<String, Method>();

	public boolean containReadField(String strField) {
		return itemToGetMethod.containsKey(strField);
	}

	public boolean containWriteField(String strField) {
		return itemToSetMethod.containsKey(strField);
	}

	/**
	 * 设置被监控的属性和其对应获取属性值的方法名
	 * 
	 * @param string
	 *            被监控的属性
	 * @param method
	 *            获取属性值的方法名
	 */
	public void putReadMethod(String string, Method method) {
		itemToGetMethod.put(string, method);
	}

	public void putWriteMethod(String string, Method method) {
		itemToSetMethod.put(string, method);
	}

	public Method getReadMethod(String strField) {
		return itemToGetMethod.get(strField);
	}

	public Method getWriteMethod(String strField) {
		return itemToSetMethod.get(strField);
	}

	public String[] getMonitoredFieldNames() {

		Object[] objects = itemToGetMethod.keySet().toArray();
		String[] strings = new String[objects.length];
		for (int i = 0; i < objects.length; i++) {
			try {
				strings[i] = new String(((String) objects[i]).getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return strings;
	}

}
